package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    ShoppingCartService shoppingCartService;

    /*
     *   添加购物车
     * */
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        //获取菜品id 或者 套餐id
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(dishId != null, ShoppingCart::getDishId, dishId)
                .eq(setmealId != null, ShoppingCart::getSetmealId, setmealId)
                .eq(ShoppingCart::getUserId, userId);
        ShoppingCart one = shoppingCartService.getOne(qw);
        //判断是否第一次添加
        if (one == null) {
            shoppingCart.setUserId(userId);
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            //返回第一次添加的数据
            return R.success(shoppingCart);
        } else {//第二次添加 数量增加
            Integer number = one.getNumber();//获取当前的数量
            one.setNumber(number + 1);//数量+1
            //通过id更新对应购物车
            shoppingCartService.updateById(one);
            //返回修改的数据
            return R.success(one);
        }
    }

    /*
     *   查询购物车
     * */
    @GetMapping("/list")
    public R list() {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> cartList = shoppingCartService.list(qw);
        return R.success(cartList);
    }

    /*
     *   取消购物车
     * */
    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart) {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        //获取菜品id 或者 套餐id
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(dishId != null, ShoppingCart::getDishId, dishId)
                .eq(setmealId != null, ShoppingCart::getSetmealId, setmealId)
                .eq(dishId != null, ShoppingCart::getDishId, dishId);
        //获取购物车对象
        ShoppingCart one = shoppingCartService.getOne(qw);
        //number-1
        Integer number = one.getNumber();
        if (number>1){//如果数量大于0 更新数量-1
            one.setNumber(number-1);
            shoppingCartService.updateById(one);
        }else {//数量等于0 数据库删除 数据
            shoppingCartService.removeById(one);
        }
        return R.success("删除成功");
    }

    /*
    *   清空购物车
    * */
    @DeleteMapping("/clean")
    public R deleteShoppingCart(){
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCartService.removeById(userId);
        return R.success("删除成功");
    }
}
